﻿#include <windows.h>
#include <Commdlg.h>
#include <time.h>
#include <stdarg.h>
#include <Mstn\MdlApi\MdlApi.h>
#if defined (OLDCODE)
#include <mdl.h>        /* MDL Library funcs structures & constants */
#include <tcb.h>        /* MicroStation terminal control block */
#include <global.h>     /* structure definitions for global data areas */
#include <dlogitem.h>   /* Dialog Box Manager structures & constants */
#include <cmdlist.h>    /* MicroStation command numbers */
#include <cexpr.h>      /* C Expression structures & constants */
#include <userfnc.h>    /* definitions for accessing MDL user functions */
#include <mselems.h>    /* structures that define MicroStation elements */
#include <rscdefs.h>    /* resource mgr structure definitions & constants */
#include <scanner.h>    /* typedefs and defines for design file scanner */
#include <keys.h>       /* MicroStation keyboard definitions */
#include <mdlio.h>      /* File I/O definitions and data structs for MDL */
#include <toolset.h>    /* MicroStation tool set definitions */
#include <string.h>
#include <dlogids.h>
#include <stdlib.h>
#endif


#include "demoUI.h"      /* Contains Dlog IDs and typedefs for this app */
#include "demoUIcmd.h"   /* Contains Command numbers for this app */

#if defined (OLDCODE)
#include <dlogman.fdf>
#include <mssystem.fdf>
#include <msoutput.fdf>
#include <msrsrc.fdf>
#include <mscexpr.fdf>
#include <msstate.fdf>
#include <mselemen.fdf>
#include <mselmdsc.fdf>
#include <msmisc.fdf>
#include <msparse.fdf>
#include <mslocate.fdf>
#include <mswindow.fdf>
#include <deprecated8_9.fdf>
#include <msvar.fdf>

#endif
#include <Mstn\MdlApi\mssystem.fdf>
#include <Mstn\MdlApi\mselems.h>
#include <Mstn\MdlApi\deprecated10_0.fdf>

USING_NAMESPACE_BENTLEY_DGNPLATFORM;
USING_NAMESPACE_BENTLEY_MSTNPLATFORM;
USING_NAMESPACE_BENTLEY_MSTNPLATFORM_ELEMENT;

#include <fstream>
using namespace std;
using namespace stdext;
HMODULE H_DLLMODULE;

#define DEMOUI_MAJOR_VERSION    1
#define DEMOUI_MINOR_VERSION    0
#define DEMOUI_PATCH_VERSION    0
#define DEMOUI_SOVERSION        1

DlogBoxInfo     dlogBoxInfo;

void* pLib = NULL;

MdlPublic  bool Verify()
{
	return true;
}


MdlPublic  void DEMOUI_openMyMainDialog
(
	WCharCP            unparsedP
)
{
	printf("调用DEMOUI_openMyMainDialog函数\n");
	if (NULL == mdlDialog_open(NULL, DIALOGID_DEMOUI))
	{
		printf("打开自定义对话框    失败\n");
		mdlOutput_rscPrintf (MSG_ERROR, NULL, MESSAGELISTID_Msgs, MSGID_DialogOpen);

		Bentley::WString cmdStr("mdl unload ");
		cmdStr.append (mdlSystem_getCurrTaskID());
		mdlInput_sendKeyin (cmdStr.GetWCharCP(), true,INPUTQ_EOQ,NULL);
	}
	else
	{
		printf("打开自定义对话框    成功\n");
	}
}


#include <Windows.h>
#include <Shlobj.h>

MdlPublic  void DEMOUI_exportHook
(
DialogItemMessage       *dimP   /* => a ptr to a dialog item message */
)
{
    dimP->msgUnderstood = TRUE;

    switch (dimP->messageType)
    {
    case DITEM_MESSAGE_BUTTON:
        {
			mdlDialog_openMessageBox (DIALOGID_MsgBoxOK,L"Call Function Failed",MessageBoxIconType::Warning);
            break;
        };

    default:
        dimP->msgUnderstood = FALSE;
        break;
    }
}



Public  bool DEMOUI_unloadFunction
(
UnloadProgramReason             unloadType
)
    {

    if (unloadType < 0)
    	{
    	return	true;
    	}

	return false;
    }


MdlPublic DialogHookInfo uHooks[]=
    {
		{ HOOKITEMID_Export, (PFDialogHook)DEMOUI_exportHook },
    };


extern "C" DLLEXPORT  void MdlMain
(
int             argc,
WCharCP         argv[]
)
{
    printf("进入MdlMain入口\n");

    RscFileHandle   rscFileH;   /* a resource file handle */
    

	if (mdlResource_openFile (&rscFileH, NULL, RSC_READ) != SUCCESS)
	{
    	printf("加载资源    失败\n");
		mdlSystem_exit(	ERROR, 1);
	}
	else
	{
    	printf("加载资源    成功\n");
	}
	
    if (mdlParse_loadCommandTable (NULL) == NULL)
	{
		// 如果加载失败，打印错误信息并退出程序。
    	printf("加载命令表    失败\n");
		mdlOutput_rscPrintf (MSG_ERROR, NULL, MESSAGELISTID_Msgs,MSGID_LoadCmdTbl);
		mdlSystem_exit(	ERROR, 1);
	}
	else
	{
		// 如果加载成功，打印成功信息。
    	printf("加载命令表    成功\n");
	}

    // mdlState_registerStringIds (MESSAGELISTID_Commands, MESSAGELISTID_Prompts);

    mdlDialog_hookPublish (sizeof (uHooks)/sizeof (DialogHookInfo), uHooks);

	// SymbolSet	    *setP;
    // setP = mdlCExpression_initializeSet (VISIBILITY_DIALOG_BOX, 0, TRUE);
    // mdlDialog_publishComplexVariable (setP, "dlogboxinfo", "dlogBoxInfo", &dlogBoxInfo);
	// printf("dlogBoxInfo.filepath:%s\n",dlogBoxInfo.filepath);

    // mdlResource_loadFromStringList(dlogBoxInfo.filepath, NULL, MESSAGELISTID_Msgs,MSGID_FileString);

	// SystemCallback::SetUnloadProgramFunction(DEMOUI_unloadFunction);
    // mdlDialog_sendMessagesOnUnload (mdlSystem_getCurrMdlDesc(),TRUE);

	MdlCommandNumber  commandNumbers [] =
	{
		{ DEMOUI_openMyMainDialog, CMD_OPEN_MYMAINDIALOG },
		0
	};
    mdlSystem_registerCommandNumbers (commandNumbers);

	mdlDialog_cmdNumQueueExt (TRUE, CMD_OPEN_MYMAINDIALOG, L"", MSInputQueuePos::INPUTQ_EOQ, FALSE);
		
    return ;
}

